<?php
/**
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1
 * @license    GNU/GPL
 */
 
// No direct access
 
defined( '_JEXEC' ) or die( 'Restricted access' );
 
jimport('joomla.application.component.controller');
 
/**
 * Jobs World Component Controller
 *
 * @package    Joomla.Tutorials
 * @subpackage Components
 */
class JobsController extends JController
{
    /**
     * Method to display the view
     *
     * @access    public
     */
    function display()
    {
        // Get the document object.
		$document	= JFactory::getDocument();

		// Set the default view name and format from the Request.
		$vName	 = JRequest::getCmd('view', 'indhent');
		$vFormat = $document->getType();
		$lName	 = JRequest::getCmd('layout', 'default');
		if ($view = $this->getView($vName, $vFormat)) {
			// Do any specific processing by view.
			switch ($vName) {
				
				default:
					$user = JFactory::getUser();
					if ($user->get('guest')) {
						// Redirect to profile page.
						$this->setRedirect(JRoute::_('index.php?option=com_users', false));
						return;
					}
					$model = $this->getModel('Jobs');
					break;
			}
		}
		// Push the model into the view (as default).
			$view->setModel($model, true);
			$view->setLayout($lName);

			// Push document object into the view.
			$view->assignRef('document', $document);
			//$view->assignRef('model', $model);
			$view->display();
    }
	
	public function createNew()
	{
		$app			= JFactory::getApplication();
		$user			= JFactory::getUser();
		
		if($user->get('guest'))
		{
			// Redirect to login page.
			$this->setRedirect(JRoute::_('index.php?option=com_users&view=login', false));
			return;
		}
		//Prepare data
		$data = array();
		$data['userid'] = $user->get('id');
		$data['title'] = JRequest::getVar('name');
		$data['address'] = JRequest::getVar('address');
		$data['guests'] = JRequest::getVar('quantity');
		$data['postcode'] = JRequest::getVar('postcode');
		$data['city'] = JRequest::getVar('city');
		$data['date'] = JRequest::getVar('datepicker2');
		$data['description'] = JRequest::getVar('description');
		// Get the model.
		
		$data['sub_food'] = json_encode(JRequest::getVar('sub_food'));
		$data['sub_food_other'] = JRequest::getVar('sub_food_other');
		$data['sub_drink'] = json_encode(JRequest::getVar('sub_drink'));
		$data['sub_drink_other'] = JRequest::getVar('sub_drink_other');
		$data['sub_room_time'] = JRequest::getVar('sub_room_time');
		$data['sub_room_country'] = JRequest::getVar('sub_room_country');
		$data['sub_rent'] = JRequest::getVar('sub_rent');
		$data['sub_rent_desc'] = JRequest::getVar('sub_rent_desc');
		$data['start_hour'] = JRequest::getVar('start_hour');
		$data['start_minute'] = JRequest::getVar('start_minute');
		
		$data['offer'] = JRequest::getVar('offer');
		
		
		$model = $this->getModel('Jobs');
		$return	= $model->saveJob($data);

		// Check for errors.
		if ($return === false) {
			// Redirect back to the form.
			//$this->setMessage(JText::_('COM_JOBS_SAVE_FAILED'), 'warning');
			//$this->setRedirect(JRoute::_('index.php?option=com_jobs&view=tilmelding', false));
			$this->setRedirect(JRoute::_('index.php?option=com_jobs&view=tilmelding&layout=fail', false));
			return false;
		}
		//$this->setMessage(JText::_('COM_JOBS_SAVE_SUCCESS'));
		//$this->setRedirect(JRoute::_('index.php?option=com_jobs', false));
		$this->setRedirect(JRoute::_('index.php?option=com_jobs&view=tilmelding&layout=success', false));
		
	}
	public function viewJob(){
		$app			= JFactory::getApplication();
		$user			= JFactory::getUser();
		$userid			= (int) JRequest::getVar('userid');
		$eventid			= (int) JRequest::getVar('eventid');
		$params    = &JComponentHelper::getParams('com_jobs');
		// Fee to view job 
		$catererfee = (int) $params->get('catererfee',1);
		//echo $userid;
		if($userid != $user->get('id'))
		{
			echo "1";
			return false;
		}
		$userid = $user->get('id');
		// Get the model.
		$model = $this->getModel('Jobs');
		$row	= $model->checkPoint($userid);
		$point	= $row->point;
		if (!$point) {
			echo "2";
			return false;
		}
		// Update caterer's point
		$point =(int) ($point - $catererfee);
		$return	= $model->calculatePointCaterer($userid,$point,$eventid);
		if (!$return) {
			echo "3";
			return false;
		}
		echo json_encode($return);
	}
 	public function removeCatererOrders() {
		$user			= JFactory::getUser();
		$removeorder			= JRequest::getVar('removeorder');
		
		$see			= JRequest::getVar('see');
		
		$model = $this->getModel('Jobs');
		$return	= $model->removeCatererOrders($user->id,$removeorder);
		if ($return === false) {
			// Redirect back to the form.
			//$this->setMessage(JText::_('COM_JOBS_SAVE_FAILED'), 'warning');
			$this->setRedirect(JRoute::_('index.php?option=com_jobs&view=indholdcaterer&see='.$see, false));
			return false;
		}
		//$this->setMessage(JText::_('COM_JOBS_SAVE_SUCCESS'));
		$this->setRedirect(JRoute::_('index.php?option=com_jobs&view=indholdcaterer&see='.$see, false));
		
	}
	
	public function rmUserOrderNew() {
		$user			= JFactory::getUser();
		$removeorder			= JRequest::getVar('removeordernew');
		$see			= JRequest::getVar('see');
		
		$model = $this->getModel('Jobs');
		$return	= $model->removeUserOrdersNew($user->id,$removeorder);
		if ($return === false) {
			// Redirect back to the form.
			//$this->setMessage(JText::_('COM_JOBS_SAVE_FAILED'), 'warning');
			$this->setRedirect(JRoute::_('index.php?option=com_jobs&view=indhold&see='.$see, false));
			return false;
		}
		//$this->setMessage(JText::_('COM_JOBS_SAVE_SUCCESS'));
		$this->setRedirect(JRoute::_('index.php?option=com_jobs&view=indhold&see='.$see, false));
		
	}
	
	public function rmUserOrderOld() {
		$user			= JFactory::getUser();
		$removeorder			= JRequest::getVar('removeorderold');
		$see			= JRequest::getVar('see');
		
		$model = $this->getModel('Jobs');
		$return	= $model->removeUserOrdersOld($user->id,$removeorder);
		if ($return === false) {
			// Redirect back to the form.
			//$this->setMessage(JText::_('COM_JOBS_SAVE_FAILED'), 'warning');
			$this->setRedirect(JRoute::_('index.php?option=com_jobs&view=indhold&see='.$see, false));
			return false;
		}
		//$this->setMessage(JText::_('COM_JOBS_SAVE_SUCCESS'));
		$this->setRedirect(JRoute::_('index.php?option=com_jobs&view=indhold&see='.$see, false));
		
	}
	
	public function updateRating() {
		$user			= JFactory::getUser();
		
		$desc			= JRequest::getVar('desc');
		$quality		= JRequest::getVar('price1');
		$service		= JRequest::getVar('price2');
		$lsUser			= JRequest::getVar('lsUser');
		$eventid 		= JRequest::getVar('eventid');
		
		$model = $this->getModel('Jobs');
		$return	= $model->updateRating($eventid,$lsUser,$service,$quality,$desc);
		
		//$this->setMessage(JText::_('COM_JOBS_SAVE_SUCCESS'));
		$this->setRedirect(JRoute::_('index.php?option=com_jobs&view=rating&eventid='.$eventid, false));
		
	}
	public function editTilmelding()
	{
		$app			= JFactory::getApplication();
		$user			= JFactory::getUser();
		
		if($user->get('guest'))
		{
			// Redirect to login page.
			$this->setRedirect(JRoute::_('index.php?option=com_users&view=login', false));
			return;
		}
		
		$id = JRequest::getVar('id');
		
		//Prepare data
		$data = array();
		$data['userid'] = $user->get('id');
		$data['id'] = $id;
		$data['title'] = JRequest::getVar('name');
		$data['address'] = JRequest::getVar('address');
		$data['guests'] = JRequest::getVar('quantity');
		$data['postcode'] = JRequest::getVar('postcode');
		$data['city'] = JRequest::getVar('city');
		$data['date'] = JRequest::getVar('datepicker2');
		$data['description'] = JRequest::getVar('description');
		// Get the model.
		
		
		$data['sub_food'] = json_encode(JRequest::getVar('sub_food'));
		$data['sub_food_other'] = JRequest::getVar('sub_food_other');
		$data['sub_drink'] = json_encode(JRequest::getVar('sub_drink'));
		$data['sub_drink_other'] = JRequest::getVar('sub_drink_other');
		$data['sub_room_time'] = JRequest::getVar('sub_room_time');
		$data['sub_room_country'] = JRequest::getVar('sub_room_country');
		$data['sub_rent'] = JRequest::getVar('sub_rent');
		$data['sub_rent_desc'] = JRequest::getVar('sub_rent_desc');
		$data['start_hour'] = JRequest::getVar('start_hour');
		$data['start_minute'] = JRequest::getVar('start_minute');
		
		$data['offer'] = JRequest::getVar('offer');
		
		
		
		$model = $this->getModel('Jobs');
		
		
		$item_user = $model->getOrder($id,$user->id);
		if(!$item_user)
		{
			$return = false;
		} else {
			$return	= $model->updateJob($data);
		}
		// Check for errors.
		if ($return === false) {
			// Redirect back to the form.
			//$this->setMessage(JText::_('COM_JOBS_SAVE_FAILED'), 'warning');
			//$this->setRedirect(JRoute::_('index.php?option=com_jobs&view=tilmelding', false));
			$this->setRedirect(JRoute::_('index.php?option=com_jobs&view=tilmelding&layout=fail', false));
			return false;
		}
		//$this->setMessage(JText::_('COM_JOBS_SAVE_SUCCESS'));
		//$this->setRedirect(JRoute::_('index.php?option=com_jobs', false));
		$this->setRedirect(JRoute::_('index.php?option=com_jobs&view=tilmelding&layout=success', false));
		
	}
	public function search() {
		$user			= JFactory::getUser();
		$post = array();
		$post['eventname'] = JRequest::getVar('eventname');
		$post['postcode'] = JRequest::getVar('postcode');
		$post['city'] = JRequest::getVar('city');
		$post['quantity'] = JRequest::getVar('quantity');
		$post['deadline'] = JRequest::getVar('datepicker');
		$uri = JURI::getInstance();
		$uri->setQuery($post);
		$uri->setVar('option', 'com_jobs');
		$uri->setVar('view', 'search');

		$this->setRedirect(JRoute::_('index.php'.$uri->toString(array('query', 'fragment')), false));
		//$this->setRedirect(JRoute::_('index.php?option=com_jobs&view=search&eventname='.$postcode.'&postcode='.$postcode.'&city='.$city.'&quantity='.$quantity.'&deadline='.$deadline.'', false));
		
	}
	public function viewInfo(){
		$app			= JFactory::getApplication();
		$template = JURI::base() . 'templates/' . $app->getTemplate();
		$user			= JFactory::getUser();
		$userid			= (int) JRequest::getVar('userid');
		$eventid			= (int) JRequest::getVar('eventid');
		$params    = &JComponentHelper::getParams('com_jobs');
		// Fee to view info
		$userfee = (int) $params->get('userfee',1);
		//echo $userid;
		if($userid != $user->get('id'))
		{
			echo "1";
			return false;
		}
		$userid = $user->get('id');
		// Get the model.
		$model = $this->getModel('Jobs');
		$row	= $model->checkPoint($userid);
		$point	= $row->point;
		if (!$point) {
			echo "2";
			return false;
		}
		// Update user's point
		$point =(int) ($point - $userfee);
		$caterers	= $model->calculatePointUser($userid,$point,$eventid);
		if (!$caterers) {
			echo "3";
			return false;
		}
		// print caterer's info
		foreach($caterers as $caterer)
		{
			$img_src = is_file($caterer->image)?'src="timthumb.php?src='.$caterer->image.'"':'src="'.$template.'/images/img-caterer-no.png"';
			$html =<<<EOF
<div class="caterer-item1">
	<h2><a href="#">{$caterer->name}</a></h2>
	<p><img width="216" height="133" {$img_src} alt="">{$caterer->description}</p>
	<p><span>Telefon:</span>&nbsp;{$caterer->phone}</p>
	<p><span>Adresse:</span>&nbsp;{$caterer->address}</p>
	<p><span>Post/ By: </span>&nbsp;{$caterer->postcode}&nbsp;{$caterer->city}</p>
</div>			
EOF;
			echo $html;
		}
	}
}
?>